package com.dy.lock.screen.component.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.CustomerAuthenticationComponent;
import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.CustomerAuthenticationInfo;
import com.dy.lock.screen.pojo.CustomerMarkInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CustomerAuthenticationService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.CustomerAuthenticationVo;
import com.google.gson.Gson;

@Component("customerAuthenticationComponent")
public class CustomerAuthenticationComponentImpl implements
		CustomerAuthenticationComponent {

	private static final Logger log = Logger
			.getLogger(CustomerAuthenticationComponentImpl.class);
	private Gson gson;

	@Resource(name = "customerAuthenticationService")
	private CustomerAuthenticationService customerAuthenticationService;

	// @Resource(name = "customerMarkRelationService")
	// private CustomerMarkRelationService customerMarkRelationService;

	public CustomerAuthenticationComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean authentication(String body, Head head) {
		CustomerAuthenticationVo vo = gson.fromJson(body,
				CustomerAuthenticationVo.class);
		if (vo != null) {
			CustomerAuthenticationInfo queryInfo = new CustomerAuthenticationInfo();
			queryInfo.setUserId(vo.getUserId());
			queryInfo.setAuthType(vo.getAuthType());
			queryInfo.setApplyStatus(Constant.PASSED);
			CustomerAuthenticationInfo passed = customerAuthenticationService
					.queryAuthPassedStatus(queryInfo);
			if (passed != null) {
				throw new ServerDisponseException(
						TransmissionCode.CUSTOMER_HAVED_AUTHENTICATION
								.getCode(),
						TransmissionCode.CUSTOMER_HAVED_AUTHENTICATION
								.getMessage());
			}
			CustomerAuthenticationInfo authInfo = new CustomerAuthenticationInfo();
			authInfo.setAuthType(vo.getAuthType());
			authInfo.setRealName(vo.getRealName());
			authInfo.setUserId(vo.getUserId());
			authInfo.setMediaAccount(vo.getMediaAccount());
			authInfo.setMobile(vo.getMobile());
			authInfo.setIdcardPicPath(vo.getIdcardPicPath());
			int result = this.customerAuthenticationService.save(authInfo);
			if (result > 0) {
				return true;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",用户认证操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public CustomerAuthenticationInfo getApplyStatus(String body, Head head) {
		CustomerAuthenticationVo vo = gson.fromJson(body,
				CustomerAuthenticationVo.class);
		if (vo != null) {
			CustomerAuthenticationInfo authInfo = new CustomerAuthenticationInfo();
			authInfo.setAuthType(vo.getAuthType());
			authInfo.setUserId(vo.getUserId());
			CustomerMarkInfo markInfo = new CustomerMarkInfo();
			markInfo.setMarkId(vo.getAuthType());
			markInfo.setUserId(vo.getUserId());
			CustomerAuthenticationInfo authDB = this.customerAuthenticationService
					.queryAuthStatus(authInfo);
			if (authDB != null) {
				return authDB;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.CUSTOMER_NOT_AUTHENTICATION.getCode(),
						TransmissionCode.CUSTOMER_NOT_AUTHENTICATION
								.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",查询认证状态时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
